package org.dromara.system.listener;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.SpringUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.excel.core.ExcelListener;
import org.dromara.common.excel.core.ExcelResult;
import org.dromara.system.domain.bo.WbDemandListBo;
import org.dromara.system.domain.bo.WbGoodsListInfoBo;
import org.dromara.system.domain.vo.WbDemandListVo;
import org.dromara.system.domain.vo.WbGoodsListInfoVo;
import org.dromara.system.service.IWbDemandListService;
import org.dromara.system.service.IWbGoodsListInfoService;

import java.util.ArrayList;
import java.util.List;

/**
 * 系统用户自定义导入
 *
 * @author Lion Li
 */
@Slf4j
public class WbGoodsListInfoImportListener extends AnalysisEventListener<WbGoodsListInfoVo> implements ExcelListener<WbGoodsListInfoVo> {

    private final IWbGoodsListInfoService wbGoodsListInfoService;

    private final String itemId;

    private int successNum = 0;
    private int failureNum = 0;
    private int updateNum = 0;
    private final StringBuilder successMsg = new StringBuilder();
    private final StringBuilder failureMsg = new StringBuilder();
    List<WbGoodsListInfoVo> list = new ArrayList<>();

    public WbGoodsListInfoImportListener(String itemId) {
        this.wbGoodsListInfoService = SpringUtils.getBean(IWbGoodsListInfoService.class);
        this.itemId = itemId;
    }

    @Override
    public void invoke(WbGoodsListInfoVo wbGoodsListInfoVo, AnalysisContext context) {
        try {
            if(StringUtils.isNotEmpty(itemId) && StringUtils.isNotEmpty(wbGoodsListInfoVo.getSubStandardId())){
                WbGoodsListInfoBo wbGoodsListInfoBo = BeanUtil.toBean(wbGoodsListInfoVo, WbGoodsListInfoBo.class);
                wbGoodsListInfoBo.setItemId(itemId);
                wbGoodsListInfoService.insertByBo(wbGoodsListInfoBo);
                successNum++;
                list.add(wbGoodsListInfoVo);
                successMsg.append("<br/>").append(successNum).append("、分标编号 ").append(wbGoodsListInfoVo.getSubStandardId()).append(" 导入成功");
            }else{
                failureNum++;
                String msg = "<br/>" + failureNum + "、分标编号 " + wbGoodsListInfoVo.getSubStandardId() + " 导入失败：";
                failureMsg.append(msg).append("无项目ID或文件缺失分标编号");
            }
        } catch (Exception e) {
            failureNum++;
            String msg = "<br/>" + failureNum + "、分标编号 " + wbGoodsListInfoVo.getSubStandardId() + " 导入失败：";
            failureMsg.append(msg).append(e.getMessage());
            log.error(msg, e);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {

    }

    @Override
    public ExcelResult<WbGoodsListInfoVo> getExcelResult() {
        return new ExcelResult<>() {

            @Override
            public String getAnalysis() {
                if (failureNum > 0) {
                    failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
                    throw new ServiceException(failureMsg.toString());
                } else {
                    successMsg.insert(0, "恭喜您，数据已全部导入成功！新增共 " + successNum + "，修改共，"+ updateNum +"条， 条，数据如下：");
                }
                return successMsg.toString();
            }

            @Override
            public List<WbGoodsListInfoVo> getList() {
                return list;
            }

            @Override
            public List<String> getErrorList() {
                return null;
            }
        };
    }
}
